﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Tools
{
    /// <summary>
    /// 单例模式运行，否则每次都要SetStandardTime来计算偏移量
    /// </summary>
    public class TimeSynchronize
    {
        private TimeSpan _timeDiff = new TimeSpan(0);

        #region kernel32.dll 静态方法，修改系统时间

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern int SetLocalTime(ref SystemTime lpSystemTime);

        //struct for date/time apis 
        struct SystemTime
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMilliseconds;
        }

        public static int SetLocalSystemTime(DateTime dateTime)
        {
            // And then set up a structure with the required properties and call the api from code: 
            SystemTime systNew = new SystemTime();

            // 设置属性 
            systNew.wDay = (ushort)dateTime.Day;
            systNew.wMonth = (ushort)dateTime.Month;
            systNew.wYear = (ushort)dateTime.Year;
            systNew.wHour = (ushort)dateTime.Hour;
            systNew.wMinute = (ushort)dateTime.Minute;
            systNew.wSecond = (ushort)dateTime.Second;
            systNew.wMilliseconds = (ushort)dateTime.Millisecond;

            // 调用API，更新系统时间 
            return SetLocalTime(ref systNew);
        }

        #endregion

        public DateTime Now
        {
            get { return DateTime.Now; }
        }

        public DateTime StandardNow
        {
            get { return DateTime.Now.Add(_timeDiff); }
        }

        public TimeSpan TimeDiff
        {
            get { return _timeDiff; }
            //set { _timeDiff = value; }
        }

        /// <summary>
        /// 设置基准时间，调整偏移量，可以多次设置
        /// </summary>
        /// <param name="dateTime"></param>
        public void SetStandardTime(DateTime dateTime)
        {
            SetStandardTime(dateTime, false);
        }

        /// <summary>
        /// 2011-01-17 增加 by 杜有发，是否同步本地时间为可选
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="syncLocalTime"></param>
        public void SetStandardTime(DateTime dateTime, bool syncLocalTime)
        {
            _timeDiff = dateTime - DateTime.Now;
            //一秒钟的ticks = 10000000

            //if (_timeDiff.Ticks < 0)//本机时间提前了 2010-10-14 不管如何都调整时间 by 杜有发
            //{

            if (syncLocalTime)
            {
                int i = SetLocalSystemTime(dateTime);

                if (i == 1)//系统时间修改成功
                    _timeDiff = new TimeSpan(0);//重新设置偏移量
                else
                {
                    //Common.DefaultLogger.Info("系统时间修改失败");
                }
            }

            //}
        }

        /*/// <summary>
        /// 获取基于标准时间的时间，自动加上偏移量
        /// </summary>
        /// <returns></returns>
        public DateTime GetStandardTime()
        {
            return Now.Add(_timeDiff);
        }*/
    }
}
